/**
 * Created by bian on 16-3-11.
 */
var util = require('util');

var auth = require('../../helpers/auth.js');
var db = require('../../models/index.js');
var render = require('../../instances/render.js');
var debug = require('../../instances/debug.js');

module.exports = (router) => {
    router.get('/adminer-super/ads',function *(){
        if((yield auth.user(this)).type !== 100){
            return;
        }
        var data = yield db.models.ads.findAll({});
        var imgUrl = [];
        for(var x in data){
            imgUrl.push(data[x].imgUrl);
        }
        this.body = yield render('admin/ads.html',{
            data:{
                imgs:JSON.stringify(imgUrl)
            }
        });
    });

    router.post('/adminer-super/ads',function *(){
        var imgs = JSON.parse(this.request.body.imgs);
        yield db.models.ads.sync({force:true});
        for(var x in imgs){
            yield db.models.ads.create({
                imgUrl:imgs[x]
            });
        }
        this.redirect('/adminer-super/ads');
    });
};